package pit.com.ordermobilesystem.util;

import org.json.JSONException;
import org.json.JSONObject;

import pit.com.ordermobilesystem.constant.OrderConstant;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

public class Utils {
	public static String getRealPathFromURI(Context context, Uri contentUri) {
		  Cursor cursor = null;
		  try { 
		    String[] proj = { MediaStore.Images.Media.DATA };
		    cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
		    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		    cursor.moveToFirst();
		    return cursor.getString(column_index);
		  } finally {
		    if (cursor != null) {
		      cursor.close();
		    }
		  }
	}
	
	public static JSONObject createDataForInsertGeneralService(JSONObject data,String table){

		try {
			JSONObject json=new JSONObject();
			json.put("table", table);
			json.put("data", data);	
			return json;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static JSONObject createDataForUpdateGeneralService(JSONObject data,String table,String id){

		try {
			JSONObject json=new JSONObject();
			json.put("table", table);
			json.put("id", id);
			json.put(id,data.getInt(id));
			data.remove(id);
			json.put("data", data);	
			return json;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String getPathImage(String url){
		return "http://"+OrderConstant.SERVER_IMAGE+"/"+OrderConstant.NAME_SERVICE+"/"+OrderConstant.IMAGE_PATH+"/"+url;
	}
	
	public static JSONObject createDataForDeleteGeneralService(JSONObject data,String table,String id){

		try {
			JSONObject json=new JSONObject();
			json.put("table", table);
			json.put("id", id);
			json.put(id,data.getInt(id));
			data.remove(id);
			return json;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}	
}
